/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.wiztools.xpathevaluator;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author subwiz
 */
public class InputPanel extends JPanel {
    
    private JTextField jtf_xpath = new JTextField(20);
    private JComboBox jcb_result_type = new JComboBox(new String[]{"String",
            "NodeList", "Boolean", "Number"});
    private JButton jb_query = new JButton("Query");
    
    private JTextArea jta_xml = new JTextArea(15, 20);
    private JScrollPane jsp_xml = new JScrollPane(jta_xml);
    
    private View view;
    
    public InputPanel(View view){
        this.view = view;
        init();
    }
    
    private void init(){
        JPanel jp = this;
        jp.setLayout(new BorderLayout(Util.WIDTH, Util.WIDTH));
        
        JPanel jp_xpath = new JPanel();
        jp_xpath.setLayout(new GridLayout(2, 1));
        jp_xpath.add(new JLabel("XPath:"));
        jp_xpath.add(jtf_xpath);
        
        JPanel jp_result_type = new JPanel();
        jp_result_type.setLayout(new GridLayout(2, 1));
        jp_result_type.add(new JLabel("Result Type:"));
        jp_result_type.add(jcb_result_type);
        
        JPanel jp_query = new JPanel();
        jp_query.setLayout(new GridLayout(2, 1));
        jp_query.add(new JLabel(""));
        jb_query.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                evaluateXPath();
            }
        });
        jp_query.add(jb_query);
        
        JPanel jp_xml = new JPanel();
        jp_xml.setLayout(new BorderLayout(Util.WIDTH, Util.WIDTH));
        jp_xml.add(new JLabel("XML:"), BorderLayout.NORTH);
        jp_xml.add(jsp_xml, BorderLayout.CENTER);
        
        JPanel jp_in_east = new JPanel();
        jp_in_east.setLayout(new BorderLayout(Util.WIDTH, Util.WIDTH));
        jp_in_east.add(jp_result_type, BorderLayout.WEST);
        jp_in_east.add(jp_query, BorderLayout.CENTER);
        
        JPanel jp_north = new JPanel();
        jp_north.setLayout(new BorderLayout(Util.WIDTH, Util.WIDTH));
        jp_north.add(jp_xpath, BorderLayout.CENTER);
        jp_north.add(jp_in_east, BorderLayout.EAST);
        
        jp.add(jp_north, BorderLayout.NORTH);
        jp.add(jp_xml, BorderLayout.CENTER);
        
    }
    
    private void evaluateXPath(){
        String xpathStr = jtf_xpath.getText();
        String xmlStr = jta_xml.getText();
        String returnTypeStr = (String)jcb_result_type.getSelectedItem();
        try{
            Object o = Util.evaluateXPath(xmlStr, xpathStr, Util.getQNameFromStr(returnTypeStr));
            view.doResult((String)o);
        }
        catch(Exception ex){
            view.doError(ex);
        }
    }
}
